/*
 * ListViewEx
 * Copyright (C) 2008, Philip Pronin (philip.pronin@gmail.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "listViewEx.h"

#include <QStandardItemModel>

ListViewEx::ListViewEx(QWidget* parent) : QWidget(parent) {
	setupUi(this);

	model = new QStandardItemModel(0, 0, this);
	listView->setModel(model);

	connect(listView->selectionModel(),
		SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
		SLOT(selectionChangedSlot(const QItemSelection &)));
	connect(listView->selectionModel(),
		SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
		SIGNAL(selectionChanged(const QItemSelection &)));
	connect(toolButtonAdd, SIGNAL(clicked()), SIGNAL(beforeUpdate()));
	connect(toolButtonAdd, SIGNAL(clicked()), SIGNAL(addClicked()));
}

QStandardItemModel* ListViewEx::getModel() const {
	return model;
}

QItemSelectionModel* ListViewEx::getSelectionModel() const {
	return listView->selectionModel();
}

void ListViewEx::deleteItem() {
	emit beforeUpdate();

	QModelIndexList indexes = listView->selectionModel()->selectedRows();
	qSort(indexes.begin(), indexes.end(), qGreater<QModelIndex>());
	foreach (QModelIndex i, indexes) {
		QList<QStandardItem*> list = model->takeRow(i.row());
		emit beforeRowDelete(list);
		foreach(QStandardItem* item, list) delete item;
	}
}

bool ListViewEx::canMoveItems(int pos) const {
	QModelIndexList indexes = listView->selectionModel()->selectedRows();
	bool ok = false;
	foreach (QModelIndex i, indexes) if (i.isValid()) {
		ok = true;
		bool canMove = i.row() + pos >= 0 &&
			i.row() + pos < listView->model()->rowCount(QModelIndex());
		if (!canMove) return false;
	}

	return ok;
}

void ListViewEx::moveItems(int pos) {
	if (!canMoveItems(pos) || !pos) return;

	emit beforeUpdate();

	QModelIndexList indexes = listView->selectionModel()->selectedRows();
	if (pos < 0) qSort(indexes.begin(), indexes.end());
	else qSort(indexes.begin(), indexes.end(), qGreater<QModelIndex>());

	bool first = true;
	int cur = listView->selectionModel()->currentIndex().isValid() ?
			listView->selectionModel()->currentIndex().row() : -1;
	foreach (QModelIndex i, indexes) {
		int row = i.row();
		QList<QStandardItem*> list = model->takeRow(row);
		model->insertRow(row + pos, list);

		#define SEL_ARGS model->index(row + pos, 0), \
			(first ? QItemSelectionModel::ClearAndSelect : QItemSelectionModel::Select) | \
			QItemSelectionModel::Rows

		if (row == cur) listView->selectionModel()->setCurrentIndex(SEL_ARGS);
		else listView->selectionModel()->select(SEL_ARGS);
		first = false;

		#undef SEL_ARGS
	}
}


void ListViewEx::upItem() {
	moveItems(-1);
}

void ListViewEx::downItem() {
	moveItems(1);
}

void ListViewEx::selectionChangedSlot(const QItemSelection& selected) {
	toolButtonDelete->setEnabled(!selected.isEmpty());
	toolButtonUp->setEnabled(canMoveItems(-1));
	toolButtonDown->setEnabled(canMoveItems(1));
}
